/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import dao.DepartmentDAO;
import dao.DesignationDAO;
import dao.EmployeeDAO;
import dao.JobrotationDAO;
import dao.SectionDAO;
import data.Departments;
import data.Designation;
import data.Employees;
import data.Jobrotation;
import data.Sections;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import org.hibernate.HibernateException;
import view.ManageJobrotation;

/**
 *
 * @author Hùng
 */
public class JobrotationBO {

    String Success;

    public JobrotationBO(String Sussecc) {
        this.Success = Sussecc;
    }

    public JobrotationBO() {
        Success = null;
    }

    public String getSussecc() {
        return Success;
    }

    public void setSussecc(String Sussecc) {
        this.Success = Sussecc;
    }

    public List<Jobrotation> showAllJob() {
        List<Jobrotation> lJob = new JobrotationDAO().getalljobrotation();
        return lJob;
    }

    public void createJobrotation(int rowCount, String idE, Date tmpdate, String remaks, Object tmpdeptName, Object secName, Object design, Object status, String txtflag) {
        //check Employee
        if (rowCount == 1) {
            //check information
            if (tmpdeptName != null && design != null && status != null && !idE.equals("") && tmpdate != null) {
                //check section
                if ((txtflag.equals("0") && secName == null) || (!txtflag.equals("0") && secName != null)) {
                    //check status and check 
                    int tmpflag = 0;
                    List<Jobrotation> lJob = new JobrotationDAO().getAllJobRotationofE(idE);
                    if (lJob != null) {
                        tmpflag = lJob.get(lJob.size() - 1).getStatus();
                    }
                    if (tmpflag == 1 || lJob == null) {
                        //set information
                        Designation tmpdesign = new DesignationDAO().getDesignationbyDesignation((String) design);
                        Employees tmpE = new EmployeeDAO().getEbyIdE(idE);
                        if(tmpE == null){
                            throw new Error("Please,check Id Employee");
                        }
                        String deptName = (String) tmpdeptName;
                        Departments tmpdept = new DepartmentDAO().getDepartmentByName(deptName);
                        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                        String date = df.format(tmpdate);
                        Sections tmpSec = new SectionDAO().getSectionbyNameSec((String) secName);
                        int tmpStatus = 0;
                        String tmpS = (String) status;
                        if (tmpS.equals("Rotate")) {
                            tmpStatus = 1;
                        } else {
                            tmpStatus = 2;
                        }
                        Jobrotation tmpJob = new Jobrotation();
                        tmpJob.setDesignation(tmpdesign);
                        tmpJob.setEmployees(tmpE);
                        tmpJob.setDepartments(tmpdept);
                        tmpJob.setSections(tmpSec);
                        tmpJob.setPresentdeptno(tmpE.getDeptno());
                        if (tmpE.getSections() != null) {
                            tmpJob.setPresentsecname(tmpE.getSections().getSectname());
                        }
                        tmpJob.setPresentdesign(tmpE.getDesignation().getDesignation());
                        tmpJob.setStatus(tmpStatus);
                        tmpJob.setDate(date);
                        tmpJob.setRemarks(remaks);
                        boolean flag = new JobrotationDAO().executeInsertHQLQuery(tmpJob);
                        if (flag == true) {
                            if (tmpStatus == 1) {
                                tmpE.setDeptno(tmpdept.getDeptno());
                                tmpE.setDesignation(tmpdesign);
                                tmpE.setSections(tmpSec);
                                switch (tmpE.getDesignation().getDesignation()) {
                                    case "manager":
                                        tmpE.setRole(1);
                                        break;
                                    case "HRmanager":
                                        tmpE.setRole(2);
                                        break;
                                    case "employee":
                                        tmpE.setRole(3);
                                        break;
                                }
                                new EmployeeDAO().executeUpdateHQLQuery(tmpE);
                            }
                            Success = "Rotation Success ";

                        } else {
                            throw new Error("Rotation Fail");
                        }
                    } else {
                        throw new Error("Please,check Employee's Jobrotations.There have a Jobrotation is waiting");
                    }
                } else {
                    throw new Error("Please,check your section");
                }
            } else {
                throw new Error("Please,check your information");
            }
        } else {
            throw new Error("Please,Search Employee");
        }
    }

    public List<Jobrotation> showAllJobRotationOfE(String idE) {
        List<Jobrotation> lJob = new JobrotationDAO().getAllJobRotationofE(idE);
        if (lJob != null) {
            return lJob;

        }
        return null;
    }

    public void RemoveJorotations(int[] selectedRows, TableModel model) {
        if (selectedRows.length > 0) {
            try {
                boolean flag = false;
                for (int i = 0; i < selectedRows.length; i++) {
                    int tmpid = (int) model.getValueAt(selectedRows[i], 0);
                    Jobrotation tmpJob = new JobrotationDAO().getJobrotationByIdJob(tmpid);
                    new JobrotationDAO().executeDeleteHQLQuery(tmpJob);
                }
                Success = "Delete Jorotations SuccessFully";
            } catch (HibernateException he) {
                throw new Error(he.getMessage());

            }
        } else {
            throw new Error("Please,Select Rows on Table to Delete");
        }


    }

    public void showAJobRotation(int[] selectedRows, TableModel model, String IdE) {
        if (selectedRows.length == 1) {
            List<Jobrotation> lJob = new JobrotationDAO().getAllJobRotationofE(IdE);
            if (selectedRows[0] == lJob.size() - 1) {
                int tmpIdJob = (int) model.getValueAt(selectedRows[0], 0);
                Jobrotation tmpJob = new JobrotationDAO().getJobrotationByIdJob(tmpIdJob);
                ManageJobrotation.setSjob(tmpJob);
            } else {
                throw new Error("Sorry,Jobrotation is rotated");
            }
        } else {
            throw new Error("Please,Select only Row on Table to Edit");
        }
    }

    public void editJobRotation(String idJob, String idE, Object tmpdeptName, Object cbsec, Object cbdesign, Date date, String remarks, Object cbstatus, String tmpflag) {
        //check information
        if (tmpdeptName != null && cbdesign != null && cbstatus != null && !idE.equals("") && date != null) {
            //check section
            if ((tmpflag.equals("0") && cbsec == null) || (!tmpflag.equals("0") && cbsec != null)) {
                Designation tmpdesign = new DesignationDAO().getDesignationbyDesignation((String) cbdesign);
                Employees tmpE = new EmployeeDAO().getEbyIdE(idE);
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                String tmpdate = df.format(date);
                Sections tmpSec = new SectionDAO().getSectionbyNameSec((String) cbsec);
                String deptnName = (String) tmpdeptName;
                Departments tmpdept = new DepartmentDAO().getDepartmentByName(deptnName);
                int tmpIdJob = Integer.parseInt(idJob);
                Jobrotation tmpJob = new JobrotationDAO().getJobrotationByIdJob(tmpIdJob);
                tmpJob.setDesignation(tmpdesign);
                tmpJob.setDepartments(tmpdept);
                tmpJob.setSections(tmpSec);
                tmpJob.setDate(tmpdate);
                tmpJob.setRemarks(remarks);
                int tmpStatus = 0;
                String tmpS = (String) cbstatus;
                if (tmpS.equals("Rotate")) {
                    tmpStatus = 1;
                } else {
                    tmpStatus = 2;
                }
                if (tmpStatus == 1) {
                    try {
                        tmpE.setDeptno(tmpdept.getDeptno());
                        tmpE.setDesignation(tmpdesign);
                        if (cbsec != null) {
                            tmpE.setSections(tmpSec);
                        } else {
                            tmpE.setSections(null);
                        }
                        new EmployeeDAO().executeUpdateHQLQuery(tmpE);
                        tmpJob.setStatus(tmpStatus);
                        new JobrotationDAO().executeUpdateHQLQuery(tmpJob);
                        Success = "Edit the Jobrotation SuccessFully ";
                    } catch (HibernateException he) {
                        throw new Error(he.getMessage());
                    }
                } else {
                    try {
                        tmpE.setDeptno(tmpJob.getPresentdeptno());
                        Designation tmpdesign1 = new DesignationDAO().getDesignationbyDesignation(tmpJob.getPresentdesign());
                        tmpE.setDesignation(tmpdesign1);
                        Sections tmpSec1 = new SectionDAO().getSectionbyNameSec(tmpJob.getPresentsecname());
                        if (cbsec != null) {
                            tmpE.setSections(tmpSec1);
                        } else {
                            tmpE.setSections(null);
                        }
                        new EmployeeDAO().executeUpdateHQLQuery(tmpE);
                        tmpJob.setStatus(tmpStatus);
                        new JobrotationDAO().executeUpdateHQLQuery(tmpJob);
                        Success = "Edit the Jobrotation SuccessFully ";
                    } catch (HibernateException he) {
                        throw new Error(he.getMessage());
                    }
                }
            } else {
                throw new Error("Please,check your section");
            }
        } else {
            throw new Error("Please,check your information");
        }

    }
}
